package com.bailong.administrator.ausedcar.ui.myfragment;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import com.bailong.administrator.ausedcar.R;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
//我的碎片
public class MyFragment extends Fragment implements View.OnClickListener {
    @Bind(R.id.ivbtn_qq)
    ImageButton ivbtnQq;
    @Bind(R.id.ivbtn_tb)
    ImageButton ivbtnTb;
    @Bind(R.id.ivbtn_wb)
    ImageButton ivbtnWb;
    @Bind(R.id.ivbtn_wx)
    ImageButton ivbtnWx;
    private View view;
    private Activity mcontext;
    private UMShareAPI mShareAPI;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mcontext = getActivity();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, container, false);

        ButterKnife.bind(this, view);
        ivbtnQq.setOnClickListener(this);
        ivbtnTb.setOnClickListener(this);
        ivbtnWb.setOnClickListener(this);
        ivbtnWx.setOnClickListener(this);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivbtn_qq:
                click(SHARE_MEDIA.QQ);

                break;
            case R.id.ivbtn_wb:
                click(SHARE_MEDIA.SINA);
                break;
            case R.id.ivbtn_wx:
                click(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.ivbtn_tb:
                click(SHARE_MEDIA.RENREN);
                break;

        }
    }

    public void click(final SHARE_MEDIA platform) {
        mShareAPI = UMShareAPI.get(mcontext);

        mShareAPI.doOauthVerify(mcontext, platform, new UMAuthListener() {
            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                mShareAPI.isInstall(mcontext, platform);
                mShareAPI = UMShareAPI.get(mcontext);
                mShareAPI.getPlatformInfo(mcontext, platform, new UMAuthListener() {
                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        Toast.makeText(mcontext, "成功", Toast.LENGTH_SHORT).show();
                        Log.i("------>",map.toString());
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {

                    }
                });
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                Toast.makeText(mcontext, "取消", Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);

    }
}
